<template>
    <div>
        <el-row style="margin-top: 30px">
			<el-col :span="24">
				<div>
					<h5 class="">模型显示</h5>
					<div class="b fx" style="border: 1px solid #d0d1d1; padding: 5px; margin-top: 10px; overflow: auto">
						<div>
							<nodeTreeVue ref="nodeTreeVueDom" />
						</div>
						<div id="modelPreview" class="modelBox">
							<canvas id="viewer"></canvas>
						</div>
					</div>
				</div>
				<div></div>
			</el-col>
		</el-row>
    </div>
</template>
<script setup>

import { reactive, ref, onMounted, computed, watch, inject, nextTick, provide, getCurrentInstance } from 'vue'
import plusBtn from '@/components/plusBtn/index.vue'
import { getAppList, getAppModuleListByAppId, openAbaqusFile, runAbaqusCommand, comsolCompute, comsolCompute2,ansysCompute } from '@/api/simulate'
import { rendererFn } from '@/threejs/model_preview.js'
import { selectAppParamTree } from '@/api/setting'
import { ElMessage, ElMessageBox } from 'element-plus'
import { debounce } from '@/utils/util.js'
import StpLoader from '@/v2/engine/StpLoader.js'
import { LoaderSettings } from '@/v2/entity/LoaderSettings'
import NormalTable from '@/components/paramsTable/NormalTable.vue'
import BindTable from '@/components/paramsTable/BindTable.vue'
import InputTable from '@/components/paramsTable/InputTable.vue'
import NodeTreeVue from '@/components/paramsTable/NodeTree.vue'
import useSidebarStatusStore from '@/stores/sidebarContrl'
import BorderTable from '@/components/paramsTable/BorderTable.vue'

const modelPath = ref('http://localhost:8087/origin/pcb-fluid.stp')


// 模型结构的树节点
const nodeTreeVueDom = ref(null)

const isComsol = ref(false)

const currentNodeKey = ref(null) //选中的节点
provide('currentNodeKey', currentNodeKey)
const cloneNodeTreeDom = ref(null)// 备份一个dom
// 点击滚动到当前模型的位置
const scrollIntoView = (selectObject) => {
	if (nodeTreeVueDom.value.treeDom.el$) {
		cloneNodeTreeDom.value = nodeTreeVueDom.value.treeDom.el$
	}
	const nodes = cloneNodeTreeDom.value.querySelectorAll('.el-tree-node')
	let curNode
	let uuid = selectObject.userData.originalMeshInstance.mesh.uuid
	nodes.forEach((item) => {
		if (item.dataset.key == uuid) {
			curNode = item
			currentNodeKey.value = uuid
		}
	})
	curNode.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
// 点击树结构，对应的mesh高亮
const onClickTreeMeshHL = (nodeID) => {
	if (dialogFormVisible.value) {
		if (stpLoader) {
			stpLoader.onTreeClickMeshHightLight(nodeID)
		}
	} else {
		onMountedstpLoader.onTreeClickMeshHightLight(nodeID)
	}
}
provide('onClickTreeMeshHL', onClickTreeMeshHL)


const form = reactive({
	simulator: '',
	simulateModule: '',
})
// 模型节点
const ModelNodes = ref([])
provide('ModelNodes', ModelNodes)
provide('appForm', form)
// 获取模型节点
const onGetModelNodes = (nodes) => {
	console.log('nodes',nodes)
	processModelNodes(nodes)
	ModelNodes.value = [nodes]
}
// 递归处理模型节点数据
const processModelNodes = (nodes) => {
	nodes = nodes.children.map((item) => {
		if (item.children) {
			if (item.children.length > 0) {
				processModelNodes(item)
			} else {
				item.label = item.alias
			}
		}
	})
}

const onTreeFitClick = (node) => {
	if (stpLoader) {
		stpLoader.treeFitMeshesCamera(node.data)
	}
	onMountedstpLoader.treeFitMeshesCamera(node.data)
}

const onTreeDisplayMeshesClick = (node) => {
	if (stpLoader) {
		stpLoader.treeDisplayMeshes(node.data)
	}
	onMountedstpLoader.treeDisplayMeshes(node.data)
}

// ~ EntityDim 设置
const entityDimSelector = ref('')

const meshTypeList = [
	{
		label: '点',
		value: 0,
	},
	{
		label: '边',
		value: 1,
	},
	{
		label: '边界',
		value: 2,
	},
	{
		label: '域',
		value: 3,
	},
]

const selectEntityDim = (entityDimIndex, tableDataIndex, scope) => {
	console.log('scope', scope)
	tableData.value[tableDataIndex].entityDim = entityDimIndex
	console.log('entityDimSelector', entityDimSelector)
}

provide('onTreeFitClick', onTreeFitClick)
provide('onTreeDisplayMeshesClick', onTreeDisplayMeshesClick)

const onMountedSetting = ref(null)
let onMountedstpLoader = null
onMounted(() => {
	//getRemoteAppList()
	// rendererFn('modelPreview', '/gltf/pcb1.gltf')
	onMountedSetting.value = new LoaderSettings('viewer', 'modelPreview')
	onMountedSetting.value.callback.onGetModelNodes = onGetModelNodes
	onMountedSetting.value.callback.onMeshDbClick = scrollIntoView
	const modelUrl = modelPath.value
	//setTimeout(()=>{
		onMountedstpLoader = new StpLoader(modelUrl, onMountedSetting.value)
	//},5000)
	
})
</script>